/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import bropsdox.Bropsdox;
import client.Client;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import util.Utilities;

/**
 *
 * @author ACRCOM
 */
public class dashboard extends JFrame implements ActionListener{
    public JLayeredPane panel;
    ImageIcon background, logo;
    public JLabel lbackground, llogo, dir;
    JButton browse, upload, download, changeDir, SignOut;
    JTextField browsePath;
    JFileChooser fc;
    JTable tabel, jTable1;
    JScrollPane pane, jScrollPane1;
    public static Client client;
    public static DefaultTableModel model;
    public static boolean SynAvailable = false;
    public static Thread synchroThread = new Thread() {
        @Override
        public void run() {
            long lastSync = System.currentTimeMillis();
            while (SynAvailable) {
                if (System.currentTimeMillis() - lastSync > Utilities.SYNC_PERIOD) {
                    client.synchronize();
                    lastSync = System.currentTimeMillis();
                }
            }
        }
    };
    
    private static dashboard Singleton = new dashboard();

    public static dashboard getSingleton(){
      if (Singleton == null){
        Singleton = new dashboard();
      }
      return Singleton;
    }
    
    public dashboard(){
        
        super("BropsDox");
        setSize(640,480);
        
        panel = new JLayeredPane();
        panel.setBounds(0,-17,640,480);

        getContentPane().setLayout(null);
        setResizable(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        background = new ImageIcon(this.getClass().getResource("/gui/dashboardbg.png"));
        
        lbackground = new JLabel(background);
        lbackground.setBounds(0, 0, 640, 480);
        panel.add(lbackground, new Integer(1));
        
        logo = new ImageIcon(this.getClass().getResource("/gui/logo.png"));
        
        llogo = new JLabel(logo);
        llogo.setSize(llogo.getPreferredSize());
        llogo.setLocation(50,50);    
        
        dir = new JLabel("Directory");
        dir.setSize(dir.getPreferredSize());
        dir.setLocation(175, 125);
                
        browse = new JButton("Browse");
        browse.setSize(browse.getPreferredSize());
        browse.setLocation(525,350);
        browse.addActionListener(this);
        
        browsePath = new JTextField(50);
        browsePath.setLocation(165, 350);
        browsePath.setSize(350, 25);
        
        upload = new JButton("Upload");
        upload.setSize(upload.getPreferredSize());
        upload.setLocation(525,400);

        download = new JButton("Download");
        download.setSize(download.getPreferredSize());
        download.setLocation(425,400);
        
        changeDir = new JButton("Change Directory");
        changeDir.setSize(changeDir.getPreferredSize());
        changeDir.setLocation(470,125);
        
        SignOut = new JButton("Sign Out");
        SignOut.setSize(SignOut.getPreferredSize());
        SignOut.setLocation(525,50);
        
        tabel = new JTable(10,4);
        tabel.setSize(tabel.getPreferredSize());
        tabel.setLocation(165,165);
//        tabel.setModel(new javax.swing.table.DefaultTableModel(
//            new Object [][] {
//                {null, null, null, null},
//                {null, null, null, null},
//                {null, null, null, null},
//                {null, null, null, null}
//            },
//            new String [] {
//                "File Name", "Title 2", "Title 3", "Title 4"
//            }
//        ));
        
        //createTable();
        panel.add(llogo, new Integer(2));
        panel.add(dir, new Integer(2));
        panel.add(browse, new Integer(2));
        panel.add(browsePath, new Integer(2));
        panel.add(upload, new Integer(2));
        panel.add(download, new Integer(2));
        panel.add(changeDir, new Integer(2));
        panel.add(SignOut, new Integer(2));
        panel.add(tabel, new Integer(2));
        //panel.add(pane, new Integer(2));
        
        //setUndecorated(true);
        //getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
        getContentPane().add(panel);
        setVisible(true);
    }

    public void createTable(){
    jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "File Name", "File Type", "File Size", "Date Modified"
            }
        ));
        jScrollPane1.setViewportView(jTable1);
        //panel.add(jTable1);
        //panel.add(jScrollPane1);
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(15, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
        );

        pack();
    }
    
    public static void updateFiles(){
        File f = new File("files.txt");
        if(f.exists()){
            PrintWriter Pw = null;
            try {
                Pw = new PrintWriter(f);
                int rowCount = model.getRowCount();
                for(int i=0; i<rowCount;++i){
                    Pw.println(model.getValueAt(i, 0));
                    Pw.println(model.getValueAt(i, 1));
                }   
                Pw.close();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Bropsdox.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == browse){
           
           fc = new JFileChooser();
           fc.showOpenDialog(this);
           fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

           int returnVal = fc.showOpenDialog(null);
                if(returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                    String text = file.getPath();
                    browsePath.setText(text);
                    String nd = browsePath.getText();
                    System.out.println(nd);
                    System.out.println(fc.getSelectedFile().getName());
                    model.addRow(new Object[]{nd,fc.getSelectedFile()});
                    updateFiles();
                }
        }
        
    }
    
    public static void main(String[] args){
        //System.out.println("lalalla");
        dashboard.getSingleton();
    }    
}
